"""
Database models for CPU, GPU, and Game lists
"""
from datetime import datetime
from sqlalchemy import create_engine, Column, Integer, String, DateTime, Enum, ForeignKey, Float
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
from config.database_config import POOL_CONFIG

Base = declarative_base()

class CPU(Base):
    __tablename__ = 'cpu_list'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    cpu_str_id = Column(String(50), unique=True, nullable=False)
    pure_name = Column(String(100), nullable=False)
    text = Column(String(200), nullable=False)
    status = Column(Integer, default=0)  # 0-新增, 1-启用, 2-忽略, 3-数据已获取
    create_time = Column(DateTime, default=datetime.now)

class GPU(Base):
    __tablename__ = 'gpu_list'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    gpu_str_id = Column(String(50), unique=True, nullable=False)
    pure_name = Column(String(100), nullable=False)
    text = Column(String(200), nullable=False)
    status = Column(Integer, default=0)  # 0-新增, 1-启用, 2-忽略
    create_time = Column(DateTime, default=datetime.now)

class Game(Base):
    __tablename__ = 'game_list'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    game_str_id = Column(String(50), unique=True, nullable=False)
    en_name = Column(String(100), nullable=False)
    cn_name = Column(String(100), nullable=False)
    match_names = Column(String(200))
    status = Column(Integer, default=0)  # 0-新增, 1-启用, 2-忽略, 3-暂不启用, 4-热门游戏
    slugs = Column(String(50), nullable=False)
    create_time = Column(DateTime, default=datetime.now)

class FPSPrediction(Base):
    __tablename__ = 'fps_predict'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    cpu_id = Column(Integer, ForeignKey('cpu_list.id'), nullable=False)
    gpu_id = Column(Integer, ForeignKey('gpu_list.id'), nullable=False)
    game_id = Column(Integer, ForeignKey('game_list.id'), nullable=False)
    resolution_type = Column(Integer, nullable=False)  # 0-1080p, 1-1440p, 2-2160p
    low_setting_fps = Column(Float, nullable=False)
    medium_setting_fps = Column(Float, nullable=False)
    high_setting_fps = Column(Float, nullable=False)
    ultra_setting_fps = Column(Float, nullable=False)
    create_time = Column(DateTime, default=datetime.now)
    
    # Relationships
    cpu = relationship("CPU")
    gpu = relationship("GPU")
    game = relationship("Game")

def init_db(db_url):
    """Initialize database connection with connection pool"""
    engine = create_engine(
        db_url,
        pool_size=POOL_CONFIG['pool_size'],
        max_overflow=POOL_CONFIG['max_overflow'],
        pool_timeout=POOL_CONFIG['pool_timeout'],
        pool_pre_ping=POOL_CONFIG['pool_pre_ping'],
        echo=POOL_CONFIG['echo'],
        pool_use_lifo=POOL_CONFIG['pool_use_lifo']
    )
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    return Session() 